﻿using MoviesWeb.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoviesWeb.DAL
{
    public class ManagerDAL
    {
        static MoviesWebEntities1 MV = new MoviesWebEntities1();
        DbSet<Manager> table = MV.Manager;
        public bool Login(string account, string password)
        {
            if (!ManagerIsExist(account))
                return false;
            Manager record = table.FirstOrDefault(p => p.Mn_account == account && p.Mn_password == password);
            return record != null;
        }
        public bool Changepwd(string account, string oldpwd, string newpwd,int id)
        {
            Manager record = table.FirstOrDefault(p => p.Mn_id == id);
            if (record == null)
                return false;
            if (record.Mn_password == oldpwd)
            {
                record.Mn_password = newpwd;
                record.Mn_account = account;
                return MV.SaveChanges() == 1;
            }
            else
                return false;
        }
        public bool ManagerIsExist(string account)
        {
            Manager record = table.FirstOrDefault(p => p.Mn_account == account);
            return record != null;
        }
        public Manager GetManager(string account)
        {
            return table.FirstOrDefault(p => p.Mn_account == account);
        }
        public List<Manager> GetManagerList()
        {
            return table.ToList();
        }
        public bool ChangeLostLoginTime(string account)
        {
            var record = GetManager(account);
            record.Mn_losttime = DateTime.Now;
            return MV.SaveChanges() == 1;
        }
        public bool Add(Manager  manager)
        {
           table.Add(manager);
           return  MV.SaveChanges() == 1;
        }
        public bool Remove(int id)
        {
            table.Remove(table.FirstOrDefault(p => p.Mn_id == id));
            return MV.SaveChanges() == 1;
        }
    }
}
